/**
 * Design a method to find the frequency of occurrences of any given word in a
 * book. What if we were running this algorithm multiple times?
 * <p>
 * You should implement following methods:
 * <p>
 * <p>
 * WordsFrequency(book) constructor, parameter is a array of strings,
 * representing the book.
 * get(word) get the frequency of word in the book.
 * <p>
 * <p>
 * Example:
 * <p>
 * <p>
 * WordsFrequency wordsFrequency = new WordsFrequency({"i", "have", "an", "apple",
 * "he", "have", "a", "pen"});
 * wordsFrequency.get("you"); //returns 0，"you" is not in the book
 * wordsFrequency.get("have"); //returns 2，"have" occurs twice in the book
 * wordsFrequency.get("an"); //returns 1
 * wordsFrequency.get("apple"); //returns 1
 * wordsFrequency.get("pen"); //returns 1
 * <p>
 * <p>
 * Note:
 * <p>
 * <p>
 * There are only lowercase letters in book[i].
 * 1 <= book.length <= 100000
 * 1 <= book[i].length <= 10
 * get function will not be called more than 100000 times.
 * <p>
 * <p>
 * Related Topics 设计 字典树 数组 哈希表 字符串 👍 38 👎 0
 */


package com.xixi.basicAlgroithms.hash;

import java.util.HashMap;
import java.util.Map;

public class ID_interview_16_02_WordsFrequencyLcci {
    public static void main(String[] args) {


    }


    //leetcode submit region begin(Prohibit modification and deletion)
    class WordsFrequency {

        Map<String, Integer> wordsMap = new HashMap<>();

        public WordsFrequency(String[] book) {

            for (int i = 0; i < book.length; i++) {
                String a = book[i];
                Integer aFrequency = wordsMap.putIfAbsent(a, 1);
                if (aFrequency != null) {
                    wordsMap.put(a, aFrequency + 1);
                }
            }

        }

        public int get(String word) {
            int res = 0;
            if (wordsMap.containsKey(word)) {
                res = wordsMap.get(word);
            }


            return res;
        }
    }

/**
 * Your WordsFrequency object will be instantiated and called as such:
 * WordsFrequency obj = new WordsFrequency(book);
 * int param_1 = obj.get(word);
 */
//leetcode submit region end(Prohibit modification and deletion)


}